package com.study.spmongodb.book.wwj.sp;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/24 23:06
 */
public class CompletableFutureAction2 {

    public static void main(String[] args) throws InterruptedException {
        AtomicBoolean finished = new AtomicBoolean(false);
        CompletableFuture.supplyAsync(CompletableFutureAction1::get)
                .whenComplete((v, t) -> {
                    Optional.ofNullable(v).ifPresent(System.out::println);
                    Optional.ofNullable(t).ifPresent(x -> x.printStackTrace());
                    finished.set(true);
                });

        System.out.println("====i am not block");
        while (!finished.get()) {
            Thread.sleep(100);
        }

        //执行完不会自动退出，ExecutorService executorService = Executors.newFixedThreadPool(2);
        final ExecutorService executorService = Executors.newFixedThreadPool(2, r -> {
            final Thread thread = new Thread();
            //后台线程
            thread.setDaemon(true);
            return thread;
        });
        executorService.execute(() -> System.out.println("test"));
        executorService.shutdown();

    }
}
